package com.seelyn.ddvideo.utils;

import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.service.VideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/21
 */
public class DownloadManager extends Thread {

    private static Logger LOG = LoggerFactory.getLogger(DownloadManager.class);
    private VideoService videoService;
    private static DownloadManager downloadManager;
    private static volatile boolean isRunning = true;


    public static DownloadManager getInstance() {
        if (downloadManager == null) {
            downloadManager = new DownloadManager();
        }
        return downloadManager;
    }

    public void setVideoService(VideoService videoService) {
        this.videoService = videoService;
    }

    public static void stopDown() {
        DownloadManager.isRunning = false;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted() && isRunning) {
            try {

                final UrlInfo info = DownloadQueue.getDownQueue().take();
                final DownloadMain downUtil = new DownloadMain(info.getUrl(), info.getTargetDir(), info.getTargetFile(), 3);
                downUtil.download(new CompleteHandler() {
                    @Override
                    public void doProcess() {
                        try {
                            System.out.println("完成");
                        } catch (Exception e) {
                            LOG.error(e.getMessage(), e);
                        }
                    }
                });

                Thread.sleep(1 * 1000);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }

        }
    }

}
